/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2015-02-11 11:23:17
 * @filename : site/nff/controller/reply.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module) {
    require('{component}promptTip/promptTip.css');
    var ReplyView, nav='reply', eId,emailType,ajaxTrue = true,
        mId = 'reply', reRender = true, menuName = 'reply',
        tpl = require('{view}reply.tpl'),
        promptTip = require('{component}promptTip/promptTip'),
        panel = require('{component}panel/zepto.panel'),
        imglazyload = require('{component}imglazyload/zepto.imglazyload'),
        tplN = require('{view}userNav.tpl');

    ReplyView = Backbone.View.extend({

        el: '',

        template: _.template(tpl),
        templateN: _.template(tplN),

        initialize: function () {},

        render: function (obj) {
            if (!share.checkPermission(true)){return;};
            share.reRender(reRender,mId);
            eId = ''; emailType = '';
            window.scroll(0,0);
            if ($('#'+mId).length < 1) { 
                $('body').append(this.template());
                this.bindEvt();            
            } else {
                $('#'+mId).css('display','block');
            }
            share.panel(panel,this.templateN,mId,menuName);
            if (obj.val) {
                var pObj = JSON.parse(obj.val);
                eId = pObj.eId;
                emailType = pObj.emailType;
                this.readPrevEmail(pObj); 
            }
        },
 
        setData : function(username,subject,text) {
            var dom = $('#'+mId);  
            if (username) {
                dom.find('.compose_name').find('input').val(username);              
            }
            if (subject) {
                dom.find('.compose_content').find('input').val(subject);              
            }
            if (text) {
                dom.find('.prevEmailContent').html(text);
            }
        },

        bindEvt : function() {
            var tthis = this,dom = $('#'+mId);    

            dom.find('.prevEmailBtn').on('tap',function(e){
                 $('#'+mId).find('.prevEmail').toggle();    
                 window.scroll(0,400);   
            });

            dom.find('.back').on('tap',function(e){
                e.stopPropagation();
                window.history.go(-1);
            }); 
               
            dom.find('.compose_add_user').on('tap',function(){
                e.stopPropagation();
                window.location.href = '#composeUser/whole';
            });

            dom.find('.save').on('tap',function(){
                //check
                var obj = {};
                obj.to = $.trim(dom.find('.compose_name').find('input').val());
                obj.subject = $.trim(dom.find('.compose_content').find('input').val());
                obj.txt  = dom.find('.compose_content').find('textarea').val();
                obj.subfolder = emailType;
                obj.send_cc_to_real_email = 0;
                obj.replied_msg_id = eId;
                obj.del_draft_msg_id = eId;
                var bool = tthis.checkEmail(obj);
                if(bool) {
                    dom.find('.promptTip').html('');
                    tthis.checkUserName(obj);
                }
            });
        },
       
        checkUserName : function(dataObj) {
            var tthis = this, 
                dom = $('#'+mId);   
            var obj = {
                url : seajs.data.vars.apiAccessUrl+'wm_user_info',
                type : 'POST',
                data : 'user='+dataObj.to,
                success : function(data) {
                    if (data.usr_id > 0) {
                        tthis.sendEmail(dataObj); 
                    } else {
                        promptTip.show({type:'error',text:data.error, dom:dom.find('.promptTip')});
                        share.pageLoad(false,$('body'));
                    }
                },
                error : function(xhr, errorType, error) {
                },
                complete : function(xhr, status) {
                }
            };
            share.pageLoad(true,$('body'));
            $.ajax(share.ajaxControl(obj)); 
        }, 
 
        readPrevEmail : function(obj) {
             var emailName =  $('#readEmail').find('.emailHead_right_name').html(),
                 emailSubject = $('#readEmail').find('.emailHead_right_line').html(),
                 emailTxt = $('#readEmail').find('.emailBody').html();
             if (emailSubject) {
                  this.setData(emailName,emailSubject,emailTxt);
             } else {
                  this.getEmail({message_id:obj.eId,subfolder:obj.emailType}); 
             }
        },
 
        checkEmail : function(obj) {
             var bool = true,
                 dom = $('#'+mId);   

             dom.find('.promptTip').html('');
             if (obj.to == ''){
                 bool = false;
                 promptTip.show({type:'error',text:'Please type a username.', dom:dom.find('.promptTip'),time:2000});
             }

             else if (obj.subject == '') {
                 bool = false;
                 promptTip.show({type:'error',text:'Please type a subject.', dom:dom.find('.promptTip'),time:2000});                   }

             else if (obj.txt == '') {
                 bool = false;
                 promptTip.show({type:'error',text:'Please type a message.', dom:dom.find('.promptTip'),time:2000});                   }
 
             return bool; 
        },

        clearEmail : function() {
             var dom = $('#'+mId); 
             dom.find('.email_to').val('');
             dom.find('.email_subject').val('');
             dom.find('.email_txt').val('');
        },

        getEmail : function(data) {
             var tthis = this;
             var obj = {
                    url : seajs.data.vars.apiAccessUrl+'wm_display_email',
                    type : 'POST',
                    data : data,
                    success : function(data) {
                        if (data.ret) {
                            tthis.setData(data.to,data.subject,data.content);
                        } 
                    },
                    error : function(xhr, errorType, error) {
                    },
                    complete : function(xhr, status) {
                        share.pageLoad(false,$('body'));
                    }
                };
             share.pageLoad(true,$('body'));
             $.ajax(share.ajaxControl(obj));
        },

        sendEmail : function(data) {
            var tthis = this , dom = $('#'+mId).find('.body_all').find('ul');
            var obj = {
                url : seajs.data.vars.apiAccessUrl+'wm_send_email',
                type : 'POST',
                data : data,
                success : function(data) {
                    if (data.ret) {
                           promptTip.show({type:'success',text:'Email successfully sent.',dom:$('#'+mId).find('.promptTip'),time:2000});                 
                           //tthis.clearEmail();
                           setTimeout(function(){
                               window.history.go(-1);
                           },2500);
                    } 
                },
                error : function(xhr, errorType, error) {
                },
                complete : function(xhr, status) {
                    share.pageLoad(false,$('body'));
                    ajaxTrue = true;
                }
            };
            share.pageLoad(true,$('body'));
            if (ajaxTrue) {
                ajaxTrue = false;
                $.ajax(share.ajaxControl(obj)); 
            }
        },
    });
    module.exports = ReplyView;
});

